#include <string>
#include "./server_Cliente.h"
#include "../../Common/Src/Http.h"

bool server_Cliente::estaActivo() {
	return this->activo;
}

server_Cliente::server_Cliente() {
	this->activo = true;
}

server_Cliente::~server_Cliente() {
	if (socket->estaActivo())
		socket->shutdown();
	delete (socket);
}

void server_Cliente::setSocket(Socket* sock) {
	this->socket = sock;
}

void server_Cliente::finalizar() {
	if (this->socket->estaActivo()) {
		this->socket->shutdown();
		this->socket->close();
	}
	this->activo = false;
}

HttpResponse* server_Cliente::procesarPedido(HttpRequest* request) {
	HttpResponse* response = new HttpResponse(request);
	std::cout << "inicio request" << std::endl;
	std::cout << "Method: " << request->getMethod() << std::endl;
	std::cout << "Host: " << request->getHost() << std::endl;
	std::cout << "Connection: " << request->getConnection() << std::endl;
	std::cout << "TypeText: " << request->getTypeText() << std::endl;
	std::cout << "Uri: " << request->getUri() << std::endl;
	std::cout << "User Agent: " << request->getUserAgent() << std::endl;
	std::cout << "fin request" << std::endl;
	return response;
}

void server_Cliente::correr() {
	Http protocolo(this->socket);
	HttpRequest *request = NULL;
	HttpResponse *response = NULL;

	//No necesito el while por que no se mantiene la conexion.
	//while (this->socket->estaActivo()) {
	request = protocolo.listenToClient();
	if (request != NULL) {
		response = procesarPedido(request);
		protocolo.enviar(response);
	}
	delete (response);
	delete (request);
	finalizar();
	//	}
}
